using Neo.Generator.CodeGen;
using Neo.MetaModel;
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using Webbness.VsTool.Interfaces;

namespace Webbness.VsTool
{
    [Serializable]
    public class SingleFileCreator : BaseTemplateCreator, ISingleTemplateCreator
    {
        protected Neo.MetaModel.Model _model;
        protected StringDictionary _properties = new StringDictionary();

        private bool GetUseBuiltins()
        {
            if (string.IsNullOrEmpty(this._properties["UseBuiltins"]))
            {
                return false;
            }
            return Convert.ToBoolean(this._properties["UseBuiltins"]);
        }

        public  void Run()
        {
            string destFileName = Path.Combine(base._outputRoot, base._template.Replace(".vtl", string.Empty));
            if (base._template.EndsWith(".vtl") && !string.IsNullOrEmpty(Path.GetExtension(destFileName)))
            {
                SolutionAndProjectContext ctx = new SolutionAndProjectContext(this._model);
                ctx.Extension = Path.GetExtension(destFileName);
                ctx.BaseDirectory = base._outputRoot;
                ctx.ProjectName = base._projectName;
                ctx.UseBuiltins = this.GetUseBuiltins();
                this.GenerateOutputFromTemplate(destFileName, base._templatesFolder, base._template, ctx);
                base._createdFiles.Add(destFileName);
            }
        }

        public  ArrayList CreatedFiles
        {
            get
            {
                return base._createdFiles;
            }
        }

        public  bool CreateFolders
        {
            get
            {
                return base._createFolders;
            }
            set
            {
                base._createFolders = value;
            }
        }

        public  bool ForceOverwrite
        {
            get
            {
                return base._forceOverwrite;
            }
            set
            {
                base._forceOverwrite = value;
            }
        }

        public  Neo.MetaModel.Model Model
        {
            get
            {
                return this._model;
            }
            set
            {
                this._model = value;
            }
        }

        public  string OutputFolder
        {
            get
            {
                return base._outputRoot;
            }
            set
            {
                base._outputRoot = value;
            }
        }

        public  string ProjectName
        {
            get
            {
                return base._projectName;
            }
            set
            {
                base._projectName = value;
            }
        }

        public  StringDictionary Properties
        {
            get
            {
                return this._properties;
            }
            set
            {
                this._properties = value;
            }
        }

        public  string Template
        {
            get
            {
                return base._template;
            }
            set
            {
                base._template = value;
            }
        }

        public  string TemplatesFolder
        {
            get
            {
                return base._templatesFolder;
            }
            set
            {
                base._templatesFolder = value;
            }
        }
    }
}

